package hexagon.security.core.domain;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author XueguiYuan
 * @version 1.0 (created time: 2016/12/3).
 */
@NoArgsConstructor
@Getter
@Setter
@ToString(of = {"name"})
@Entity
@Table(name = "scope")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Scope extends SecurityAbstractEntity {

    @NotNull
    private String name;

    @Transient
    public abstract Scope getParent();

    @Transient
    public abstract List<? extends Scope> getChildren();

    public boolean contains(Scope scope) {
        if (scope == null) {
            return false;
        }
        if (equals(scope)) {
            return true;
        }
        if (getChildren().contains(scope)) {
            return true;
        }
        return contains(scope.getParent());
    }
}
